﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DumplicateFuntionDemo
{
    /// <summary>
    /// ArrowButton1.xaml 的交互逻辑
    /// </summary>
    public partial class ArrowButton1 : UserControl
    {
        public ArrowButton1()
        {
            InitializeComponent();
            Loaded += (s, e) => UpdateAllLines();
            SizeChanged += (s, e) => UpdateAllLines();
        }

        // 依赖属性定义
        public static readonly DependencyProperty Number1Property =
            DependencyProperty.Register("Number1", typeof(string), typeof(ArrowButton1), new PropertyMetadata("1"));
        public static readonly DependencyProperty Number2Property =
            DependencyProperty.Register("Number2", typeof(string), typeof(ArrowButton1), new PropertyMetadata("2"));
        public static readonly DependencyProperty Number3Property =
            DependencyProperty.Register("Number3", typeof(string), typeof(ArrowButton1), new PropertyMetadata("3"));
        public static readonly DependencyProperty Number4Property =
            DependencyProperty.Register("Number4", typeof(string), typeof(ArrowButton1), new PropertyMetadata("4"));
        // Number2-4属性省略...

        public static readonly DependencyProperty LineOrientationProperty =
            DependencyProperty.Register("LineOrientation", typeof(string), typeof(ArrowButton1),
                new PropertyMetadata("Vertical", OnLineOrientationChanged));

        public static readonly DependencyProperty DashedLineDirectionProperty =
            DependencyProperty.Register("DashedLineDirection", typeof(string), typeof(ArrowButton1),
                new PropertyMetadata("LeftToRight", OnDashedDirectionChanged));

        // 坐标依赖属性
        public static readonly DependencyProperty PrimaryLineStartProperty =
            DependencyProperty.Register("PrimaryLineStart", typeof(Point), typeof(ArrowButton1));
        public static readonly DependencyProperty PrimaryLineEndProperty =
            DependencyProperty.Register("PrimaryLineEnd", typeof(Point), typeof(ArrowButton1));
        public static readonly DependencyProperty SecondaryLineStartProperty =
            DependencyProperty.Register("SecondaryLineStart", typeof(Point), typeof(ArrowButton1));
        public static readonly DependencyProperty SecondaryLineEndProperty =
            DependencyProperty.Register("SecondaryLineEnd", typeof(Point), typeof(ArrowButton1));
        public static readonly DependencyProperty DashedLineStartProperty =
            DependencyProperty.Register("DashedLineStart", typeof(Point), typeof(ArrowButton1));
        public static readonly DependencyProperty DashedLineEndProperty =
            DependencyProperty.Register("DashedLineEnd", typeof(Point), typeof(ArrowButton1));
        public static readonly DependencyProperty ArrowheadPointsProperty =
            DependencyProperty.Register("ArrowheadPoints", typeof(string), typeof(ArrowButton1));


        public Point PrimaryLineStart
        {
            get => (Point)GetValue(PrimaryLineStartProperty);
            set => SetValue(PrimaryLineStartProperty, value);
        }
        public Point PrimaryLineEnd
        {
            get => (Point)GetValue(PrimaryLineEndProperty);
            set => SetValue(PrimaryLineEndProperty, value);
        }
        public Point SecondaryLineStart
        {
            get => (Point)GetValue(SecondaryLineStartProperty);
            set => SetValue(SecondaryLineStartProperty, value);
        }
        public Point SecondaryLineEnd
        {
            get => (Point)GetValue(SecondaryLineEndProperty);
            set => SetValue(SecondaryLineEndProperty, value);
        }


        public Point DashedLineStart
        {
            get => (Point)GetValue(DashedLineStartProperty);
            set => SetValue(DashedLineStartProperty, value);
        }

        public Point DashedLineEnd
        {
            get => (Point)GetValue(DashedLineEndProperty);
            set => SetValue(DashedLineEndProperty, value);
        }

        public string ArrowheadPoints
        {
            get => (string)GetValue(ArrowheadPointsProperty);
            set => SetValue(ArrowheadPointsProperty, value);
        }



        public string LineOrientation
        {
            get => (string)GetValue(LineOrientationProperty);
            set => SetValue(LineOrientationProperty, value);
        }
        public string DashedLineDirection
        {
            get => (string)GetValue(DashedLineDirectionProperty);
            set => SetValue(DashedLineDirectionProperty, value);
        }

        public string Number1
        {
            get => (string)GetValue(Number1Property);
            set => SetValue(Number1Property, value);
        }
        public string Number2
        {
            get => (string)GetValue(Number2Property);
            set => SetValue(Number2Property, value);
        }
        public string Number3
        {
            get => (string)GetValue(Number3Property);
            set => SetValue(Number3Property, value);
        }
        public string Number4
        {
            get => (string)GetValue(Number4Property);
            set => SetValue(Number4Property, value);
        }

        // ... existing code ...
        public static readonly DependencyProperty ArrowOffsetProperty =
            DependencyProperty.Register("ArrowOffset", typeof(double), typeof(ArrowButton1), new PropertyMetadata(0.0));

        // ... existing code ...
        public double ArrowOffset
        {
            get => (double)GetValue(ArrowOffsetProperty);
            set => SetValue(ArrowOffsetProperty, value);
        }

        // ... existing code ...
        private void UpdateArrowhead()
        {
            if (LineOrientation == "Vertical")
            {
                SetValue(ArrowheadPointsProperty,
                    $"{ActualWidth * 0.9 - 5 + ArrowOffset},{ActualHeight * 0.2} " +
                    $"{ActualWidth * 0.9 + ArrowOffset},{ActualHeight * 0.2} " +
                    $"{ActualWidth * 0.9 - 5 + ArrowOffset},{ActualHeight * 0.2 + 10}");
            }
            else
            {
                SetValue(ArrowheadPointsProperty,
                    $"{ActualWidth * 0.8},{ActualHeight * 0.9 - 5 + ArrowOffset} " +
                    $"{ActualWidth * 0.8},{ActualHeight * 0.9 + ArrowOffset} " +
                    $"{ActualWidth * 0.8 + 10},{ActualHeight * 0.9 - 5 + ArrowOffset}");
            }
        }
        // ... existing code ...

        // 属性变更回调
        private static void OnLineOrientationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is ArrowButton1 control) control.UpdateAllLines();
        }

        private static void OnDashedDirectionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is ArrowButton1 control) control.UpdateDashedLine();
        }

        // 核心更新逻辑
        private void UpdateAllLines()
        {
            if (ActualWidth <= 0 || ActualHeight <= 0) return;

            // 根据方向更新主线条
            if (LineOrientation == "Vertical")
            {
                // 垂直线布局
                PrimaryLineStart = new Point(ActualWidth * 0.1, ActualHeight * 0.8);
                PrimaryLineEnd = new Point(ActualWidth * 0.1, ActualHeight * 0.2);
                SecondaryLineStart = new Point(ActualWidth * 0.9, ActualHeight * 0.2);
                SecondaryLineEnd = new Point(ActualWidth * 0.9, ActualHeight * 0.8);
            }
            else
            {
                // 水平线布局
                PrimaryLineStart = new Point(ActualWidth * 0.2, ActualHeight * 0.1);
                PrimaryLineEnd = new Point(ActualWidth * 0.8, ActualHeight * 0.1);
                SecondaryLineStart = new Point(ActualWidth * 0.8, ActualHeight * 0.9);
                SecondaryLineEnd = new Point(ActualWidth * 0.2, ActualHeight * 0.9);
            }

            UpdateDashedLine();
            UpdateArrowhead();
        }

        private void UpdateDashedLine()
        {
            if (LineOrientation == "Vertical")
            {
                // 垂直布局下的虚线方向
                DashedLineStart = DashedLineDirection == "LeftToRight" // 
                    ? new Point(ActualWidth * 0.2, ActualHeight * 0.2) // 垂直布局下的虚线方向
                    : new Point(ActualWidth * 0.8, ActualHeight * 0.2); // 
                DashedLineEnd = DashedLineDirection == "LeftToRight" // 垂直布局下的虚线方向
                    ? new Point(ActualWidth * 0.8, ActualHeight * 0.8) // 
                    : new Point(ActualWidth * 0.2, ActualHeight * 0.8); // 
            }
            else
            {
                // 水平布局下的虚线方向
                DashedLineStart = DashedLineDirection == "TopToBottom"
                    ? new Point(ActualWidth * 0.2, ActualHeight * 0.1)
                    : new Point(ActualWidth * 0.2, ActualHeight * 0.9);
                DashedLineEnd = DashedLineDirection == "TopToBottom"
                    ? new Point(ActualWidth * 0.8, ActualHeight * 0.9)
                    : new Point(ActualWidth * 0.8, ActualHeight * 0.1);
            }
        }
    }
}